package com.ruoyi.util;

import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Random;

/**
 * TODO
 *
 * @author chenchuan2
 * @date 2023-01-16 13:47:05
 */
public class CertSign {
    public static void main(String[] args) throws Exception {
        String nonce = String.format("%d", Math.abs(new Random().nextInt()));
        String timestamp = Long.toString(System.currentTimeMillis() / 1000);
        String securityKey = "abc";
        System.out.println("sign:" + genCertSign(nonce, timestamp, securityKey));
    }

    public static String genCertSign(String nonce, String timeStamp, String securityKey) throws Exception {
        String[] signElement = {nonce, timeStamp, securityKey};
        Arrays.sort(signElement);
        String rawSign = String.join("", signElement);
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.update(rawSign.getBytes("UTF-8"), 0, rawSign.length());
        byte[] sign = md.digest();

        StringBuilder sb = new StringBuilder();
        for (byte b : sign) {
            sb.append(String.format("%02x", b));
        }
        return sb.toString();
    }
}
